package com.mew.chess.core.game.bjl.game;

import cn.hutool.core.util.StrUtil;
import com.mew.chess.core.common.handler.ChessStart;
import com.mew.chess.core.common.handler.ChessStartAnnotation;
import com.mew.chess.core.common.utils.ChessGameUtil;
import com.mew.chess.core.common.utils.RobotNumberUtils;
import com.mew.chess.core.game.bjl.game.common.BjlConstantValue;
import com.mew.chess.core.game.bjl.game.common.BjlLinkType;
import com.mew.chess.core.game.bjl.game.message.UserMessage;
import com.mew.common.dc.redis.GameRoomRedisService;
import com.mew.common.entity.config.Room;
import com.zebra.common.concurrent.ZebraExecutorManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@ChessStartAnnotation("BJL")
@Service
@Slf4j
public class BjlStartService implements ChessStart {
    @Autowired
    private GameRoomRedisService gameRoomRedisService;

    @Override
    public void initializeGame(int gameId) throws Exception {
        BjlGameManager.getInstance().setGameId(gameId);
        addRoomManager(gameId);
        log.info("-------BJL---gameId:{}---start-success-----", gameId);
    }

    private void addRoomManager(int gameId)throws Exception{
        List<Room> roomList = gameRoomRedisService.findRoomByGameId(gameId);
        if (null == roomList || roomList.isEmpty()){
            throw new Exception("Start Game "+ gameId +" room is null");
        }

        for (Room room : roomList){
            initializeRoomManager(room);
        }
    }

    private void initializeRoomManager(Room room){
        BjlRoomManager roomController = new BjlRoomManager(room.getGameId(), room.getId());
        roomController.setIno(ChessGameUtil.generateINO(0));

        roomController.setBetStrs(room.getBetArrs());
        roomController.setBetArrays(StrUtil.splitToInt(room.getBetArrs(), ","));
        roomController.setMinGoldNumber(room.getMinGoldNumber());

        roomController.setLinkType(BjlLinkType.Prepare.code());
        roomController.setRuleTime(BjlConstantValue.PREPARE_TIME);
        roomController.setBjlScheduleManager(new BjlScheduleManager(room.getId(), ZebraExecutorManager.getInstance().getDefaultScheduler(), ZebraExecutorManager.getInstance().getDefaultExecutor()));

        //最大压注限制
        roomController.setMaxBetLimit(roomController.getBetArrays()[0]*BjlConstantValue.MAX_BET_LIMIT);

        initializePlayerList(roomController);
        initializeBigPlayer(roomController);

        BjlGameManager.getInstance().putController(roomController);
    }

    private void initializePlayerList(BjlRoomManager roomController){
        int robotNum= RobotNumberUtils.robotNumber(BjlConstantValue.ROBOT_INIT_NUMBER);
        for (int i = 0; i < robotNum; i++) {
            UserMessage user = UserMessage.buildBjlRobotUserMessage(roomController.getMinGoldNumber(), BjlConstantValue.ROBOT_GOLD_SEC);
            roomController.getPlayerList().put(user.getUserId(),user);
        }
        roomController.setRobotNumber(robotNum);
    }
    /**
     * 初始化大玩家
     * @return
     */
    public void initializeBigPlayer(BjlRoomManager roomController){
        for (UserMessage u : roomController.getPlayerList().values()) {
            roomController.getBigRList().put(u.getUserId(),u);
            if(roomController.getBigRList().size()>=BjlConstantValue.BIG_PLAYER_NUMBER){
                return;
            }
        }
    }
}
